<!--数据库维护-->
<div class="row">
    <!-- 映射列表 -->
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="leftTree">
        <div class="widget flat radius-bordered">
            <div class="widget-header bg-sky theme">
                <span class="widget-caption">文件维护</span>
            </div>
            <div class="widget-body no-padding">
                <form id="aceEditParamForm" action="switchView.do?viewPath=common/aceEditor/aceEdit.html" method="post" target="aceEditIframe" class="hidden" accept-charset="utf-8">
                    <input type="hidden" name="sorceTreeViewUrl" value="${ctx}/preserve/file/projectDirTree.do">
                    <!--左侧资源树视图路径-->
                    <input type="hidden" name="mode" value='${mode}'>
                    <!--编辑器编程语言模式-->
                    <input type="hidden" name="callback" value='loadFileTreeCallback'>
                    <!--资源树加载成功后的回调函数-->
                    <input type="hidden" name="loadSelectFile" value='loadSelectFile'>
                    <!--点击保存时的的回调函数-->
                    <input type="hidden" name="saveCallback" value='saveFile'>
                    <!--点击删除时的的回调函数-->
                    <input type="hidden" name="deletFileCallback" value='deletSelectFile'>
                    <!--点击重命名时的的回调函数-->
                    <input type="hidden" name="renameFileCallback" value='renameSelectFile'>
                    <!--点击保存时的的回调函数-->
                    <input type="hidden" name="editType" value='file'>
                    <#--编辑器类型-->
                </form>
                <div id="aceEditLoadHint" style="text-align: center">加载中...</div>
                <form id="downloadFileIframe" action="${ctx}/preserve/database/saveSql.do" method="post" target="downloadFileIframe" style="display: none">
                    <input type="hidden" name="sql">
                </form>
                <iframe style="width: 100%; height: 91%;border:none;" name="aceEditIframe"/>
                <iframe name="downloadFileIframe" style="display: none;"></iframe>
            </div>
        </div>
    </div>
</div>
<script>
    //自动提交表单
    setTimeout(function () {
        console.log($("#aceEditParamForm").size());
        $("#aceEditLoadHint").remove();
        $("#aceEditParamForm").submit();
    }, 100)
    function loadFileTreeCallback(result) {
        console.log("数据库表加载结果回调:" + result);
    }
    //加载文件内容
    function loadSelectFile(showFileContent) {
        var editFrame = window.frames["aceEditIframe"];
        if (editFrame) {
            var selectFile = editFrame.$("#projectDirTree").tree("selectedItems");
            if (selectFile && selectFile.length > 0) {
                Notify('正在加载文件内容...', 'bottom-right', '1000', 'success', 'fa-info', true);
                var filePath = selectFile[0].params.path;
                ajaxRequest("${ctx}/preserve/file/redFile.do", {filePath: filePath}, function (html) {
                    showFileContent(html);
                    var suffix = filePath.substr(filePath.lastIndexOf(".") + 1);
                    suffix = suffix == "js" ? "javascript" : suffix;
                    suffix = suffix == "jspf" ? "jsp" : suffix;
                    suffix = suffix == "tld" ? "xml" : suffix;
                    editFrame.$("#editorMode").val(suffix).change();
                    console.log(suffix);
                    Notify('加载文件成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                }, function (XMLHttpRequest, textStatus, errorThrown) {
                    ajaxError(errorThrown);
                }, "GET", "text");
            } else {
                Notify('请选择文件再点击加载!', 'bottom-right', '1000', 'success', 'fa-info', true);
            }
        }
    }
    //点击保存时的的回调函数
    function saveFile(content) {
        console.log("正在保存文件...");
        var editFrame = window.frames["aceEditIframe"];
        if (editFrame) {
            var selectFile = editFrame.$("#projectDirTree").tree("selectedItems");
            if (selectFile && selectFile.length > 0) {
                var filePath = selectFile[0].params.path;
                Notify('正在保存文件...', 'bottom-right', '1000', 'success', 'fa-info', true);
                ajaxRequest("${ctx}/preserve/file/saveFile.do", {filePath: filePath, newContent: content}, function (result) {
                    if (result.resCode == 200) {
                        Notify('保存成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                    } else {
                        Notify('保存失败' + (result.resMsg ? ("(" + result.resMsg + ")") : ""), 'bottom-right', '1000', 'danger', 'fa-info', true);
                    }
                }, function (XMLHttpRequest, textStatus, errorThrown) {
                    ajaxError(errorThrown);
                }, "POST", "json");
            }else{
                Notify('请选择文件', 'bottom-right', '1000', 'danger', 'fa-edit', true);
            }
        }
    }
    //删除文件
    function deletSelectFile(e,filePath) {
        var editFrame = window.frames["aceEditIframe"];
        if (editFrame) {
            var selectFile = editFrame.$("#projectDirTree").tree("selectedItems");
            if (selectFile && selectFile.length > 0) {
                var filePath = selectFile[0].params.path;
                Notify('正在删除文件...', 'bottom-right', '1000', 'success', 'fa-info', true);
                ajaxRequest("${ctx}/preserve/file/deleteFile.do", {filePath: filePath}, function (result) {
                    if (result.resCode == 200) {
                        Notify('删除成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                    } else {
                        Notify('删除失败' + (result.resMsg ? ("(" + result.resMsg + ")") : ""), 'bottom-right', '1000', 'danger', 'fa-info', true);
                    }
                }, function (XMLHttpRequest, textStatus, errorThrown) {
                    ajaxError(errorThrown);
                }, "GET", "json");
            }else{
                Notify('请选择文件', 'bottom-right', '1000', 'danger', 'fa-edit', true);
            }
        }
    }

    //重命名文件
    function renameSelectFile(e,filePath) {
        bootbox.dialog({
            size: "small",
            title: "请输入文件名称",
            message: "<input type='text' id='fileName' class='form-control'>",
            buttons: {
                confirm: {
                    label: '重命名',
                    className: 'btn-success',
                    callback: function (result) {
                        var fileName = $("#fileName").val();
                        if (fileName) {
                            var editFrame = window.frames["aceEditIframe"];
                            if (editFrame) {
                                var selectFile = editFrame.$("#projectDirTree").tree("selectedItems");
                                if (selectFile && selectFile.length > 0) {
                                    var filePath = selectFile[0].params.path;
                                    Notify('正在重命名文件...', 'bottom-right', '1000', 'success', 'fa-info', true);
                                    ajaxRequest("${ctx}/preserve/file/renameFile.do", {filePath: filePath,fileName:fileName}, function (result) {
                                        if (result.resCode == 200) {
                                            Notify('重命名成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                                        } else {
                                            Notify('重命名失败' + (result.resMsg ? ("(" + result.resMsg + ")") : ""), 'bottom-right', '1000', 'danger', 'fa-info', true);
                                        }
                                    }, function (XMLHttpRequest, textStatus, errorThrown) {
                                        ajaxError(errorThrown);
                                    }, "GET", "json");
                                }else{
                                    Notify('请选择文件', 'bottom-right', '1000', 'danger', 'fa-edit', true);
                                }
                            }
                        }else{
                            Notify('请输入文件名称', 'bottom-right', '1000', 'danger', 'fa-edit', true);
                        }
                    }
                },
                cancel: {
                    label: '取消',
                    className: 'btn-danger'
                }
            }
        });
    }
</script>


